package com.llkj.contractmanagement.contractmanagement.contract;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.adapter.SystemMessageAdapter;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.main.MyApplication;
import com.llkj.contractmanagement.model.SystemMessageBean;
import com.llkj.contractmanagement.utils.GsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import cn.bingoogolapple.refreshlayout.BGARefreshViewHolder;

/**
 * author: dong.yj
 * created on: 2016/8/17 10:27
 * description:系统通知界面
 */
public class SystemMessageActivity extends BaseActivity implements AdapterView.OnItemClickListener, BGARefreshLayout.BGARefreshLayoutDelegate {

    @Bind(R.id.title_system_message)
    TitleBar titleBar;
    @Bind(R.id.bga_refresh)
    BGARefreshLayout mRefreshLayout;
    @Bind(R.id.lv_system_message)
    ListView lv_system_message;
    private SystemMessageAdapter adapter;
    private List<SystemMessageBean.XxlistBean> messageList;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_system_message;
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
    }

    private void init() {
        initData();
        setListener();

    }

    private void initData() {
        mRefreshLayout.setRefreshViewHolder(new BGANormalRefreshViewHolder(MyApplication.getInstance(), true));
        messageList = new ArrayList<>();
        adapter = new SystemMessageAdapter(this, messageList);
        lv_system_message.setAdapter(adapter);
        HttpMethod.message(this, this, null, UrlConfig.HTTP_MESSAGE);
    }


    private void setListener() {
        mRefreshLayout.setDelegate(this);
        titleBar.setTopBarClickListener(this);
        lv_system_message.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, SystemMessageDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("messageList", messageList.get(position));
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_MESSAGE:
                mRefreshLayout.endRefreshing();
                SystemMessageBean systemMessageBean = GsonUtil.GsonToBean(responseInfo, SystemMessageBean.class);
                if (systemMessageBean != null) {
                    List<SystemMessageBean.XxlistBean> addlist = systemMessageBean.getXxlist();
                    if (addlist != null && addlist.size() > 0) {
                        messageList.addAll(addlist);
                        adapter.notifyDataSetChanged();
                    }
                }
                break;
        }
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        messageList.clear();
        HttpMethod.message(this, this, null, UrlConfig.HTTP_MESSAGE);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }
}
